package com.hxzy.common.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.controller.vue.dictdata.resp.SysDictDataResp;
import com.hxzy.controller.vue.mapstruct.SysDictDataConvert;
import com.hxzy.entity.SysDictData;
import com.hxzy.entity.SysDictType;
import com.hxzy.service.SysDictDataService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author wy
 * @Description 字典数据缓存工具类
 * @Date 2023/9/4 9:16
 * @Version 1.0
 */
@Component
public class DictDataCacheUtil  {


    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private RedisTemplate  redisTemplate;


    /**
     * spring bean生命周期，只会在bean创建的时候执行一次
     */
    @PostConstruct
    public void initMethod(){
       this.loadAllDataToRedis();
    }

    /**
     * 加载所有的数据到redis中
     * 如果不管理你启动多少次，这个方法只能够执行一次，只能加分布式锁(redission)
     */
    public void loadAllDataToRedis(){
        LambdaQueryWrapper<SysDictData> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getStatus,"0");
        queryWrapper.orderByAsc(SysDictData::getDictType)
                .orderByAsc(SysDictData::getDictSort);
        queryWrapper.select(SysDictData::getId,SysDictData::getDictSort,SysDictData::getDictLabel,
                SysDictData::getDictValue, SysDictData::getDictType,SysDictData::getListClass,SysDictData::getCssClass,SysDictData::getIsDefault);

        //查询数据
        List<SysDictData> sysDataList = this.sysDictDataService.list(queryWrapper);
        //转换
        List<SysDictDataResp> convertList = SysDictDataConvert.INSTANCE.convert(sysDataList);

        //jdk 8 按照DictType类型来分组
        Map<String, List<SysDictDataResp>> classifiedData = convertList.stream()
                .collect(Collectors.groupingBy(SysDictDataResp::getDictType));

        //循环遍历存储到redis中
        for(String key : classifiedData.keySet()){
            String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,key);
            this.redisTemplate.opsForValue().set(redisKey, classifiedData.get(key));
        }
    }

    /**
     * 移出所有的通用字典的值
     */
    public void removeAllRedisDictValue(){
        //dict_data:*
        String redisKey=RedisConstans.DICT_DATA_KEY+"*";
        Set keys = this.redisTemplate.keys(redisKey);
        this.redisTemplate.delete(keys);
    }

    /**
     * 根据键查询缓存中的值
     * @param dictType
     * @return
     */
    public List<SysDictDataResp> findByDictType(String dictType){
        String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        return (List<SysDictDataResp>) this.redisTemplate.opsForValue().get(redisKey);
    }

    /**
     * 为某一个键进行重新赋值
     * @param dictType
     * @param list
     * @return
     */
    public boolean writeDictType(String dictType,List<SysDictDataResp> list){
        String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        this.redisTemplate.opsForValue().set(redisKey,list);
        return true;
    }

    /**
     * 根据类型删除某一个redis的键
     * @param dictType
     */
    public void removeByDictType(String dictType){
        String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        this.redisTemplate.delete(redisKey);
    }

}
